Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: don't add reordered join with extra filters to original memo group #88779

Merged
merged 1 commit into from
Oct 2, 2022

Conversation

DrewKimball
Copy link
Collaborator

@DrewKimball DrewKimball commented Sep 27, 2022

The JoinOrderBuilder builds reordered join plans from the bottom up.
It expects filters to be pushed down as far as possible at each step, and
that transitive closure has been calculated over Inner Join equality filters
(e.g. a=b and b=c => a=c). It also reuses the original matched joins
when possible to avoid duplicate work by adding to the original memo groups.

This could previously cause filters to be dropped in the case when the
original join tree did not compute transitive closure and push filters down
as far as possible. More specifically, the JoinOrderBuilder could add new
reordered joins with new filters synthesized and pushed down as far as possible
to an original memo group that didn't have one of those filters. Subsequent
joins would then expect the filter to be part of the memo group, and so it
wouldn't be added later on in the plan. In the rare case when the expression
without the filter was chosen, this could manifest as a dropped filter in the
final plan. This was rare because dropping a filter usually does not produce
a lower-cost plan.

As an example, take this original join tree:

(xy join ab on true) join uv on x = u and a = u;

Here it is possible to sythesize and push down a x = a filter, and so the
JoinOrderBuilder would do this and add it to the group:

group (xy join ab on true), (xy join ab on x = a)

Later joins would use this group as an input, an expect the x = a filter to
be present. If costing happened to choose the first expression in the group,
we would end up choosing a plan like this:

(xy join ab on true) join uv on x = u

Where the a = u filter isn't included in the top-level join because it
would be redundant to add it when x = u and x = a are already present.
This is a bit of a simplification, but is essentially the problem fixed
by this commit.

This commit adds a check to the JoinOrderBuilder to identify cases where
filters (including ones sythesized from the transitive closure) weren't
pushed all the way down in the original join tree. When this is true, none
of the originally matched joins can be reused when reordered joins are
built except for the root join. This solution may perform some duplicate
work when filters aren't pushed down, but it shouldn't matter because this
case is rare (and should be avoided whenever possible).

Fixes #88659

Release note (bug fix): Fixed a bug introduced in 20.2 that could cause
filters to be dropped from a query plan with many joins in rare cases.

@DrewKimball DrewKimball requested a review from a team as a code owner September 27, 2022 01:10
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job tracking this down!

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)


-- commits line 2 at r1:
nit: consider framing this as something other than "pushdown" - see my other comment


-- commits line 15 at r1:
I think an example like you gave in our session today would be helpful - especially if we come back and read this months or years later. Also, I think it'd be helpful to call out explicitly that this happens when a join with a synthesized filter gets added to an existing memo group that has an expression where the synthesized filter was not created (pushed-down). And that this is only possible when that original expression in the group is chosen as part of the optimal plan, which is rare.


pkg/sql/opt/xform/join_order_builder.go line 367 at r1 (raw file):

		// Ensure that if any of the joins did not successfully push filters down as
		// far as possible, all joins apart from the root join will be rebuilt.
		jb.ensurePushdown()

I think framing this as a problem with "pushdown" is a little confusing, even if that is the root cause. It seems like what we are actually checking is that all edges did not synthesize filters not in the original join. So maybe there is a way to rename this and reword the comments here and below? I think you can still mention the pushdown as a cause for when this would happen, but in theory there could be other reasons we end up synthesizing a filter that wasn't in the original join.


pkg/sql/opt/xform/join_order_builder.go line 395 at r1 (raw file):

		e := &jb.edges[i]
		isFilter := len(e.filters) > 0 && e.filters[0].ScalarProps().OuterCols.Contains(3) && e.filters[0].ScalarProps().OuterCols.Contains(9)
		_ = isFilter

nit: isFilter looks like leftover code from debugging


pkg/sql/opt/xform/join_order_builder.go line 695 at r1 (raw file):

		// This inferred filter was not pushed down as far as possible. All joins
		// apart from the root will have to be rebuilt.
		jb.rebuildAllJoins = true

Is this part needed, or is it an optimization to avoid some of the work in ensurePushdown if we can prove we need to rebuild all joins here?


pkg/sql/opt/xform/join_order_builder.go line 1406 at r1 (raw file):

// was already present in the original join tree. If so, enumerating this join
// would be redundant, so it should be skipped.
func (e *edge) joinIsRedundant(jb *JoinOrderBuilder, s1, s2 vertexSet) bool {

nit: might be simpler to make this a method of JoinOrderBuilder instead of edge now.

nit: Add a note explaining that if rebuildAllJoins is true then none of new joins are redundant with original joins.


pkg/sql/opt/xform/testdata/rules/join_order line 2918 at r1 (raw file):

			(t3.a) = (t4.b)
			AND (t2.b) = (t4.a)
			AND (t2.a) = (t4.a);

nit: tabs to spaces

@DrewKimball
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Sep 30, 2022

👎 Rejected by too few approved reviews

@DrewKimball
Copy link
Collaborator Author

Oops, wrong PR.

Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner)


-- commits line 2 at r1:

Previously, mgartner (Marcus Gartner) wrote…

nit: consider framing this as something other than "pushdown" - see my other comment

Done, let me know what you think.


-- commits line 15 at r1:

Previously, mgartner (Marcus Gartner) wrote…

I think an example like you gave in our session today would be helpful - especially if we come back and read this months or years later. Also, I think it'd be helpful to call out explicitly that this happens when a join with a synthesized filter gets added to an existing memo group that has an expression where the synthesized filter was not created (pushed-down). And that this is only possible when that original expression in the group is chosen as part of the optimal plan, which is rare.

Added an example. Done.


pkg/sql/opt/xform/join_order_builder.go line 367 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I think framing this as a problem with "pushdown" is a little confusing, even if that is the root cause. It seems like what we are actually checking is that all edges did not synthesize filters not in the original join. So maybe there is a way to rename this and reword the comments here and below? I think you can still mention the pushdown as a cause for when this would happen, but in theory there could be other reasons we end up synthesizing a filter that wasn't in the original join.

I've changed the name to validateEdges and expanded the comments. LMK what you think.


pkg/sql/opt/xform/join_order_builder.go line 395 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: isFilter looks like leftover code from debugging

Oops, good catch. Done.


pkg/sql/opt/xform/join_order_builder.go line 695 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Is this part needed, or is it an optimization to avoid some of the work in ensurePushdown if we can prove we need to rebuild all joins here?

It's necessary since we set the op for the edge to the original join to which the filter would have been pushed if it existed in the original join tree, so the applicability check would always succeed. I've added a comment.


pkg/sql/opt/xform/join_order_builder.go line 1406 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: might be simpler to make this a method of JoinOrderBuilder instead of edge now.

nit: Add a note explaining that if rebuildAllJoins is true then none of new joins are redundant with original joins.

Done.


pkg/sql/opt/xform/testdata/rules/join_order line 2918 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: tabs to spaces

Done.

@DrewKimball
Copy link
Collaborator Author

TFTR! I know it's a bit tough to wade through this code.

@DrewKimball DrewKimball changed the title opt: don't add reordered join to group when filters weren't pushed down opt: don't add reordered join with extra filters to original memo group Sep 30, 2022
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

:lgtm:

Reviewed 1 of 2 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball and @mgartner)


-- commits line 15 at r1:

Previously, DrewKimball (Drew Kimball) wrote…

Added an example. Done.

Nice!


-- commits line 16 at r2:
nit: "and so wouldn't add" -> I think you're missing a word or two there?


-- commits line 38 at r2:
nit: "correct" is confusing given that the plan is incorrect, maybe "is essentially the problem fixed by this commit" or something


pkg/sql/opt/xform/join_order_builder.go line 695 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

It's necessary since we set the op for the edge to the original join to which the filter would have been pushed if it existed in the original join tree, so the applicability check would always succeed. I've added a comment.

I see. So it seems like this is required to fix this exact bug, while validateEdges provides extra safety for other cases that we aren't currently aware of. is that correct?

The `JoinOrderBuilder` builds reordered join plans from the bottom up.
It expects filters to be pushed down as far as possible at each step, and
that transitive closure has been calculated over Inner Join equality filters
(e.g. `a=b` and `b=c` => `a=c`). It also reuses the original matched joins
when possible to avoid duplicate work by adding to the original memo groups.

This could previously cause filters to be dropped in the case when the
original join tree did not compute transitive closure and push filters down
as far as possible. More specifically, the `JoinOrderBuilder` could add new
reordered joins with new filters synthesized and pushed down as far as possible
to an original memo group that didn't have one of those filters. Subsequent
joins would then expect the filter to be part of the memo group, and so it
wouldn't be added later on in the plan. In the rare case when the expression
without the filter was chosen, this could manifest as a dropped filter in the
final plan. This was rare because dropping a filter usually does not produce
a lower-cost plan.

As an example, take this original join tree:
```
(xy join ab on true) join uv on x = u and a = u;
```
Here it is possible to sythesize and push down a `x = a` filter, and so the
`JoinOrderBuilder` would do this and add it to the group:
```
group (xy join ab on true), (xy join ab on x = a)
```
Later joins would use this group as an input, an expect the `x = a` filter to
be present. If costing happened to choose the first expression in the group,
we would end up choosing a plan like this:
```
(xy join ab on true) join uv on x = u
```
Where the `a = u` filter isn't included in the top-level join because it
would be redundant to add it when `x = u` and `x = a` are already present.
This is a bit of a simplification, but is essentially the problem fixed
by this commit.

This commit adds a check to the `JoinOrderBuilder` to identify cases where
filters (including ones sythesized from the transitive closure) weren't
pushed all the way down in the original join tree. When this is true, none
of the originally matched joins can be reused when reordered joins are
built except for the root join. This solution may perform some duplicate
work when filters aren't pushed down, but it shouldn't matter because this
case is rare (and should be avoided whenever possible).

Fixes cockroachdb#88659

Release note (bug fix): Fixed a bug introduced in 20.2 that could cause
filters to be dropped from a query plan with many joins in rare cases.
Copy link
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner)


-- commits line 16 at r2:

Previously, mgartner (Marcus Gartner) wrote…

nit: "and so wouldn't add" -> I think you're missing a word or two there?

Done.


-- commits line 38 at r2:

Previously, mgartner (Marcus Gartner) wrote…

nit: "correct" is confusing given that the plan is incorrect, maybe "is essentially the problem fixed by this commit" or something

Done.


pkg/sql/opt/xform/join_order_builder.go line 695 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I see. So it seems like this is required to fix this exact bug, while validateEdges provides extra safety for other cases that we aren't currently aware of. is that correct?

Exactly.

@DrewKimball
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 1, 2022

Build failed:

@DrewKimball
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 2, 2022

Build succeeded:

@blathers-crl
Copy link

blathers-crl bot commented Oct 2, 2022

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 73e8b28 to blathers/backport-release-21.2-88779: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 21.2.x failed. See errors above.


error creating merge commit from 73e8b28 to blathers/backport-release-22.1-88779: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@DrewKimball
Copy link
Collaborator Author

blathers backport 22.2.0

@DrewKimball
Copy link
Collaborator Author

blathers backport 22.1

@blathers-crl
Copy link

blathers-crl bot commented Nov 10, 2022

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 73e8b28 to blathers/backport-release-22.1-88779: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.1 failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

roachtest/costfuzz: join reordering issue
3 participants